package com.example.newdemo.view.popup;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.newdemo.BaseFragment;
import com.example.newdemo.MainActivity;
import com.example.newdemo.R;
import com.example.newdemo.utils.LogUtils;

public class PopupViewDemoFragment extends BaseFragment implements View.OnClickListener {


    private PopupWindow mPopWindow;
    private Button showPopup;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_popup_window_demo;
    }

    @Override
    protected void initView() {
        showPopup = rootView.findViewById(R.id.btn_show_popup_window);

        showPopup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindowDrop();
            }
        });

    }

    @Override
    protected void initData() {

    }


    private void showPopupWindow() {
        //设置contentView
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.view_popup_content_layout, null);
        mPopWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);


        //设置各个控件的点击响应
        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);

        //显示PopupWindow
        View rootview = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_popup_window_demo, null);
        //相对于父控件的位置（例如正中央Gravity.CENTER，下方Gravity.BOTTOM等），可以设置偏移或无偏移
        mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 500, 500);
//        mPopWindow.showAsDropDown(rootview);

    }


    private void showPopupWindowDrop(){
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.view_popup_content_layout,null);
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);


        mPopWindow.setFocusable(true);
//        mPopWindow.setTouchable(true);
//        mPopWindow.setOutsideTouchable(true);


        //设置各个控件的点击响应
        TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);
        TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);
        TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        tv3.setOnClickListener(this);


        mPopWindow.showAsDropDown(showPopup);

        //设置动画所对应的style
        mPopWindow.setAnimationStyle(R.style.pupEnterExit);
        mPopWindow.update();    // 必须加这一句 ， 否则没有动画效果


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.pop_computer:
                LogUtils.d("  - -  computer ");
                mPopWindow.dismiss();

            break;
            case R.id.pop_financial:
                LogUtils.d("  * *  金融  ");
                mPopWindow.dismiss();

            break;
            case R.id.pop_manage:
                LogUtils.d("  + +   管理  ");
                mPopWindow.dismiss();

            break;
        }
    }



}
